<?php
/*
* 批量自动发送邮件
*/
set_time_limit(0);
require_once dirname(__FILE__) . '/mysql.php';
require_once dirname(__FILE__) . '/mailer.php';

##
#DB
##
$db = new mysql(array(
    'Mhost'=>'localhost',
    'Muser'=>'root',
    'Mpassword'=>'toor',
    'Mdbname'=>'fenfentuan',
    'Mcharset'=>'utf8')
);

##
#MAIL配置，随机一台SMTP
##
$serverNum = rand(1, 5);
$addr = 'service@fft'.$serverNum.'.com';#邮箱帐号
$pass = 'KTL_SERVICE';#邮箱密码

$name = '纷纷团';#邮箱名称
$reply = 'fenfentuan@126.com';#回复帐号

$host = 'mail.fft'.$serverNum.'.com';#SMTP服务器地址
$char = 'UTF-8';#邮件编码
$port  = 25;#SMTP端口

##
#MAIL声明
##
$mail = new PHPMailer();
$mail->IsSMTP();

//$mail->SMTPDebug = true;
//$mail->SMTPAuth   = true;

$mail->Host = $host;
$mail->Port = $port;

$mail->CharSet = $char;
$mail->Username = $addr;
$mail->Password = $pass;
$mail->SetFrom($addr, $name);
$mail->AddReplyTo($reply, $name);

##
#SEND
##
$tm = time();
$daytime = strtotime(date('Ymd'));
$num = 500;
$from = ceil(($tm - $daytime) / 1200) * $num;#每半小时发５００封
//$from = 2;
$mails = $db->select("select * from `subscribe` where 1 order by id desc limit {$from},".$num);

if($mails){
	$hours = date('H');
	if($hours == '08' || $hours == '13'){
		$mails[] = array('email'=>'union100@sina.com', 'secret'=>'hahaha1233211234567');
		$mails[] = array('email'=>'953079782@qq.com', 'secret'=>'hahaha1233211234567');
	}
    $content = file_get_contents('http://www.fenfentuan.com/cron/get_mail.php');
    if($content){
    	$ct = explode('<br/>', $content, 2);
        $mail->Subject = $ct[0];
        //$mail->Subject = '12月真情大回馈，纷纷团再推新玩意';
        $content = $ct[1];
        foreach($mails as $key=>$val){
        	$content = str_replace('<[{CoDe}]>', $val['secret'], $content);
            $mail->MsgHTML($content);
            $mail->ClearAddresses();
            $mail->AddAddress($val['email']);
            $mail->Send();//发送
            //sleep(rand(1, 2));
            echo $val['email']."<br />\n";
        }
        echo $key+1;
    }else{
        exit('none');
    }
}





?>